package com.mit.wx.wx;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mit.wx.wx.dto.BasicWxResponse;
import com.mit.wx.wx.dto.CustomerServiceAccount;
import com.mit.wx.wx.dto.CustomerServiceMessage;
import com.mit.wx.util.HttpUtil;

import java.io.IOException;
import java.util.List;

/**
 * 2018/10/11 Create by 郭文梁
 * WxKfApi
 * 微信客服API封装
 *
 * @author 郭文梁
 * @date 2018/10/11
 */
public class WxKfApi {
    private static final String DEFAULT_CHARSET = "UTF-8";
    private static final String CREATE_KF_ACCOUNT_API = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token=%s";
    private static final String MODIFY_KF_ACCOUNT_API = "https://api.weixin.qq.com/customservice/kfaccount/update?access_token=%s";
    private static final String DELETE_KF_ACCOUNT_API = "https://api.weixin.qq.com/customservice/kfaccount/del?access_token=%s";
    private static final String LIST_KF_ACCOUNT_API = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=%s";
    private static final String CUSTOMER_SERVICE_MESSAGE_API = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s";


    public static BasicWxResponse createCustomerService(String accessToken, CustomerServiceAccount account) throws IOException {
        return post(CREATE_KF_ACCOUNT_API, accessToken, account);
    }

    public static BasicWxResponse modifyCustomerService(String accessToken, CustomerServiceAccount account) throws IOException {
        return post(MODIFY_KF_ACCOUNT_API, accessToken, account);
    }

    public static BasicWxResponse deleteCustomerService(String accessToken, CustomerServiceAccount account) throws IOException {
        return post(DELETE_KF_ACCOUNT_API, accessToken, account);
    }

    public static List<CustomerServiceAccount> getCustomerServiceList(String accessToken) throws IOException {
        String resp = get(LIST_KF_ACCOUNT_API, accessToken);
        if (resp == null) {
            return null;
        }
        JSONObject respJson = JSON.parseObject(resp);
        JSONArray kfList = respJson.getJSONArray("kf_list");
        return kfList.toJavaList(CustomerServiceAccount.class);
    }

    public static BasicWxResponse sendKfMessage(String accessToken, CustomerServiceMessage message) throws IOException {
        return post(CUSTOMER_SERVICE_MESSAGE_API, accessToken, message);
    }

    private static String get(String apiUrl, String accessToken) throws IOException {
        return HttpUtil.get(String.format(apiUrl, accessToken));
    }

    private static <T> T post(String apiUrl, String accessToken, String postBody, Class<T> clazz) throws IOException {
        String url = String.format(apiUrl, accessToken);
        String resp = HttpUtil.postWithStringBody(url, postBody, DEFAULT_CHARSET);
        return JSON.parseObject(resp, clazz);
    }

    private static BasicWxResponse post(String apiUrl, String accessToken, String postBody) throws IOException {
        return post(apiUrl, accessToken, postBody, BasicWxResponse.class);
    }

    private static BasicWxResponse post(String apiUrl, String accessToken, Object postBody) throws IOException {
        return post(apiUrl, accessToken, JSON.toJSONString(postBody));
    }
}
